package com.qianchang.microfinance.setting;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.PackageUtils;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.AboutInfo;
import com.qianchang.microfinance.utils.AppUpdateManager;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 关于
 * 修 改 人: zhoulai
 * 修改时间: 2017/6/30 16:30
 */


public class AboutActivity extends BaseActivity {

    private static final String TAG = "AboutActivity";

    @BindView(R.id.tv_version)
    DTextView tvVersion;
    @BindView(R.id.tv_new_version)
    DTextView tvNewVersion;
    @BindView(R.id.tv_call)
    DTextView tvCall;
    @BindView(R.id.iv_new)
    ImageView ivNew;
    @BindView(R.id.ll_update)
    LinearLayout llUpdate;

    @BindView(R.id.tv_progress)
    DTextView tvProgress;
    @BindView(R.id.fl_progress)
    FrameLayout flProgress;

    private boolean hasNewVersion;
    private AboutInfo mAboutInfo;
    private String appUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.about));
        tvVersion.setTextContent(getString(R.string.app_name) + "APP  V" + PackageUtils.getVersion(this));
        checkVersion();
    }

    /**
     * 1	sourceType	String	25	N	来源客户端类型---1 安卓 2苹果
     * 2	userType	String	2	N	 用户类型  1客户端  2 办公端
     * 3	mandatoryUpdate	String	2	N	是否强制更新
     */
    private void checkVersion() {
        show();
        HttpParams params = new HttpParams();
        params.put("sourceType", "1");   // 2018.3.9 px
        params.put("userType", "1");
//        params.put("mandatoryUpdate", "0");
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_ABOUT, params, new RequestCallBack<AboutInfo>() {
                    @Override
                    public void onSuccess(JsonResult<AboutInfo> response) {
                        dismiss();
                        if (response.isReqDataSuccess()) {
                            mAboutInfo = response.getObject();
                            if (mAboutInfo != null) {
                                appUrl = mAboutInfo.getAppUrl();
                                tvCall.setText(response.getObject().getCustomService());
                                tvCall.setTextContent(mAboutInfo.getCustomService());
                                if (AppUpdateManager.needUpdate(mAboutInfo)) {
                                    hasNewVersion = true;
                                    tvNewVersion.setTextContent(getString(R.string.find_new_version));
                                    tvNewVersion.setTextColor(Color.RED);
                                    ivNew.setVisibility(View.VISIBLE);
                                } else {
                                    ivNew.setVisibility(View.GONE);
                                    tvNewVersion.setTextColor(getResources().getColor(R.color.common_black_color_6));
                                }
                            }
                        } else {
                            ToastUtil.getInstant().show(AboutActivity.this, response.getDescription());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        dismiss();
                        ToastUtil.getInstant().show(AboutActivity.this, msg);
                    }

                    @Override
                    public void onConnectFailure(String msg) {
                    }
                }
        );
    }

    /**
     * 拨打客服电话
     */

    private void dial() {
        String phone = tvCall.getText().toString().trim();
        phone = getString(R.string.service_line);
        if (TextUtils.isEmpty(phone)) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        startActivity(intent);
    }

    @OnClick({R.id.ll_update, R.id.tv_call})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_update:
                if (hasNewVersion) {
//                if (true) {
                    checkPermission(new CheckPermListener() {
                                        @Override
                                        public void superPermission() {
                                            Bundle bundle = new Bundle();
                                            // bundle.putString("url", DMConstant.Config.BASE_USER + appUrl);  // 旧的更新版本代码 2018.3.9 px
                                            bundle.putString("url", DMConstant.Config.FILE_IP+appUrl);
                                            DownLoadingDialog downLoadingDialog = DownLoadingDialog.newInstance(bundle);
                                            downLoadingDialog.show(getSupportFragmentManager(), getClass().getSimpleName());
                                        }

                                        @Override
                                        public void permissionSomeDeny() {
                                        }
                                    }, getString(R.string.app_name),
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE);
                } else {
                    ToastUtil.getInstant().show(this, "已是最新版本");
                }
                break;
            case R.id.tv_call:
                dial();
                break;
        }
    }

}
